const https = require('https');
const buffer = require('buffer')
var fs = require('fs')
var path = require('path')
// let axios = require('axios')
const axios = require('./utils/http.js')
var xlsx = require('node-xlsx');
const cheerio = require('cheerio');
const userAgents = require('./utils/userAgent.js');
const nodeXlsx = require('node-xlsx')
var request = require("request");
// axios.defaults.withCredentials = true
const http = require('http');
//maste
let titles = []
let times = []
let page = 1
let countList = [129, 139, 132, 143, 136, 131, 128, 145, 133, 102]
let count = 129
// let guName = ''
// let code = ''
let pageLimit = 500 //请求页数限制
var timer = null
var pageTimer = null
const agent = new https.Agent({
    rejectUnauthorized: false
});


//下方ex1是读取出来的数组，数组长度取决于Excel文件的工作表(sheet)
// const ex1 = nodeXlsx.parse("./260gu.xlsx")	//读取excel表格


// let excel_content = ex1[0].data	//取出excel文件中的第一个工作表中的全部数据
const queryUrl = 'http://www.innojoy.com/client/interface.aspx'
const loginUrl = 'http://www.innojoy.com/front/user/userManager'
const loginParams = {
    language: 'zh',
    requestModule: 'UserManager',
    systemConfig: {
        SystemTitle: null
    },
    userConfig: {
        Action: 'Login',
        AreaCode: "86",
        ChkCode: '',
        EMail: "tylgdx6@163.com",
        GUID: '',
        OperType: '',
        Password: 'joy230217',
        remember: true,
        userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
    }
}
const queryParams = {
    language: 'zh',
    patentSearchConfig: {
        Action: 'Search',
        AddOnes: '',
        DBOnly: 0,
        Database: "fmzl,fmsq,syxx,wgzl,twpat,hkpat,mopat",
        DelOnes: '',
        GUID: '',
        IsPatentList: true,
        Page: "1",
        PageSize: '10',
        query: '张本红',
        RemoveOnes: '',
        SmartSearch: '',
        Sortby: '',
        TreeQuery: '',
        TrsField: ''
    },
    requestModule: 'PatentSearch',
    userId: "E5D73753-440C-476E-9E76-B067F4041220"
}

const login = (loginParams) => {
    return new Promise((resolve, reject) => {
        axios.post(loginUrl, loginParams).then(res => {
            resolve(res)
        }).catch(err => {
            reject('登陆错误')
        })
    })
}

const queryList = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            headers: {
                'content-type': 'application/json',
                'referer': 'http://www.innojoy.com/searchresult/default.html'
            },
            method: 'post',
            url: queryUrl,
            data: params
        }).then(res => {
            resolve(res)
        })
    })
}

let dwUrl = 'http://www.innojoy.com/client/interface.aspx'
var companyName = ''
var fileName
let companyIndex = 0
let data = nodeXlsx.parse('./data.xls');
let execlData = []
let temObj = {}
var execlIndex = 10262
// execl 数据去重
data[0].data.forEach(item => {
    if (!temObj[item[1]]) {
        temObj[item[1]] = 1
        execlData.push(item)
    }
})
execlData.shift()

var getDownUrl = (dwParam) => {
    ++execlIndex
    console.log(execlIndex, companyName)
    return new Promise((resolve, reject) => {
        axios.post(dwUrl, dwParam).then(res => {
            // console.log(dwParam)
            if (res && res.ErrorInfo) {
                reject(res.ErrorInfo)
                console.log('错误信息')
            }
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}

var getfileByUrl = (url) => {
    const dest = path.join(__dirname+'/execlFile', fileName+'.xls');
    // 你可能需要自行确保该路径存在
    const file = fs.createWriteStream(dest);
    http.get(url,(res) => {
        res.pipe(file)
    })
}
let type = 1 // 1-个人 2-企业
var dwParam

const startD = () => {
    companyName = execlData[execlIndex][1]
    if (execlData[execlIndex][1].length > 3) {
        type = 2
    } else {
        type = 1
    }
    if (execlData[execlIndex][1].indexOf('*')>-1) {
        ++execlIndex
        startD()
        console.log('主体不能含有特殊符号')
        return
    }
    fileName = execlData[execlIndex][0] + '_' + execlData[execlIndex][1] + '_' + type + '_' + new Date().getTime()
    dwParam = {
        requestModule: "MyDownload",
        userId: "E5D73753-440C-476E-9E76-B067F4041220",
        myDownloadConfig: {
            Action: "AddDownloadTask",
            DownloadConfig:
                `{\"Type\":\"range\",\"StartIndex\":\"1\",\"StopIndex\":\"10\",\"Content\":{\"Bibliographic\":true,\"BookTop\":false,\"Abstract\":false,\"CL\":false,\"LS\":false,\"BookAll\":false,\"AbstImage\":false,\"CLM\":false,\"DESCR\":false,\"Key\":[\"DN\",\"TI\",\"ABST\",\"CL\",\"KEYWORDCN\",\"PA\",\"SPATMS\",\"PAN\",\"AR\",\"PACC\",\"ADDRP\",\"ADDRC\",\"ADDRDC\",\"PPA\",\"PPACC\",\"TOC\",\"RO\",\"SQRLX\",\"PAS\",\"CAS\",\"SCASTMS\",\"CAR\",\"CASP\",\"CASC\",\"CASD\",\"INN\",\"INNN\",\"PINN\",\"AGC\",\"SAGC\",\"AGT\",\"EXM\",\"IDX\",\"IDXStar\",\"REFBYN\",\"REFBSCYN\",\"REFP\",\"REFPN\",\"REFPCN\",\"REFNP\",\"REFNPN\",\"PAT\",\"IPCBN\",\"IPCSCN\",\"CLMN\",\"ICLMN\",\"CLZS\",\"DPN\",\"CHQ\",\"RET\",\"SFMLN\",\"SFMLCN\",\"IFMLN\",\"IFMLCN\",\"ISPCT\",\"ORG4\",\"WINPS\",\"WINPSY\",\"EAR\",\"CRAI\",\"INV\",\"TRA\",\"ZRR\",\"SRR\",\"ZRZXR\",\"ZRA\",\"SRA\",\"LIC\",\"XKR\",\"BXKR\",\"XKBAR\",\"PLE\",\"CZR\",\"ZQR\",\"ZYBAR\",\"AN\",\"ANSimple\",\"ADY\",\"AD\",\"PNM\",\"PDY\",\"PD\",\"SSGGR\",\"SQGGR\",\"QLFQR\",\"FMLPNMS\",\"SFMLC\",\"DAN\",\"ODAN\",\"PR\",\"EPRD\",\"EPRC\",\"IAN\",\"IAD\",\"IPN\",\"IPD\",\"DEN\",\"ETSIBZ\",\"BZH\",\"BZC\",\"BZD\",\"IPC\",\"PIPC\",\"IPCC\",\"IPCSC\",\"ICG\",\"ICSG\",\"IPCN\",\"LOC\",\"LOCN\",\"CPC\",\"CPCN\",\"CPCSETS\",\"SEC\",\"ECN\",\"UC\",\"UCN\",\"FIC\",\"FICN\",\"FTERM\",\"SBJA\",\"GBC\",\"PGBC\",\"GBCN\",\"GBC1\",\"GBC2\",\"GBC3\",\"GBC4\",\"EINDC\",\"EINDCN\",\"PTEN\",\"PB\",\"PI\",\"PF\",\"PRT\",\"PST\",\"CLS\",\"CLSD\",\"SCSC\",\"SCXX\",\"LS\",\"CO\",\"RAN\",\"CFP\"],\"FolderFieldKey\":{}},\"Formatter\":\"Excel(*.xls)\",\"Title\":\"${fileName}\",\"Lang\":\"ZH\",\"Language\":\"zh\",\"DocMerged\":true,\"emailComfig\":\"\",\"isCC\":false,\"PDFNameRule\":\"AN+TI\",\"PDFCatalog\":true,\"QueryString\":\"'${companyName}'\",\"SmartSearch\":\"\",\"TrsDb\":\"mopat,hkpat,twpat,fmsq,wgzl,syxx,fmzl\",\"TrsField\":\"\",\"PatentCount\":10,\"Sort\":\"\"}`
        },
        patentSearchConfig: {
            GUID: "NONE-4c23be42ffef4066bbc93741a6f5399c"
        },
        language: "zh"
    }
    let dUrl = ''
    getDownUrl(dwParam).then(res => {
        dUrl = 'http://www.innojoy.com' + res.Option.replace("..", "")
        console.log(dwParam)
        getfileByUrl(dUrl)
        if (execlIndex < execlData.length) {
            timer = setTimeout(() => {
                startD()
            }, Math.random() * 2 * 1000)
        } else {
            console.log('文件全部下载完成')
        }
    }).catch(err => {
        if (err==='说明书不存在') {
            ++execlIndex
            startD()
        } else {
            setTimeout(() => {
                --execlIndex
                console.log(execlIndex, companyName)
                // ++execlIndex
                startD()
            },1000 * 60 * 5)
        }
        console.log(err, '错误')
    })
}
startD()



// login(loginParams).then(res => {
//     console.log(res)
//     if (res) {
//         queryList(queryParams).then(queryRes => {
//             console.log(queryRes,'hhh')
//         })
//     }
// })

const getPUrl = (code, page) => {
    return 'https://hotels.ctrip.com/hotels/list?countryId=1&city=-1&checkin=2023/03/16&checkout=2023/03/17&optionId=11&optionType=Province&directSearch=1&optionName=%E6%B0%91%E5%AE%BF&display=%E6%B0%91%E5%AE%BF&crn=1&adult=1&children=0&searchBoxArg=t&travelPurpose=0&ctm_ref=ix_sb_dl&domestic=1&'
}
const getHtml = (url) => {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            // httpsAgent: agent,
            // headers: {
            //     'User-Agent': (new (require('user-agents'))).data.userAgent
            // }
        }).then(res => {
            resolve(res)
        }).catch(() => {
            reject('error-fetch')
        })
    })
}
let cIndex = 0
const writeDir = (guName) => {
    const path1 = `${__dirname}/execlFile/${guName}`
    var buffer
    var excelData = []
    var addInfo = {};
    //名称
    addInfo.name = guName;
    //数据数组
    addInfo.data = [
        ["序号", "股名", "评论标题", '时间'],
    ];
    fs.mkdir(path1, err => {
        titles.forEach((item, index) => {
            if (err) throw err
            addInfo.data.push([index + 1, guName, item, times[index]]);
        })
        //添加数据
        excelData.push(addInfo);
        // 写xlsx
        buffer = xlsx.build(excelData)
        try {
            //写入数据
            fs.writeFile(`${path1}/data.xls`, buffer, function (err) {
                if (err) {
                    throw err;
                }
                titles = []
                times = []
                page = 1
                ++count
                ++cIndex
                count = countList[cIndex]
                clearTimeout(timer)
                if (cIndex === 10) {
                    return
                }
                caclCount()
                console.log('Write to xls has finished');
            });
        } catch (err) { console.log(err) }
    })
}

const runningGetHtml = (guName, code) => {
    let url = getPUrl(code, page)
    page++
    console.log(page, 'page')
    getHtml(url).then(res => {
        const $ = cheerio.load(res);
        $('.articleh .l3 a').each((i, el) => {
            titles.push($(el).text())
        })
        $('.articleh .l5').each((i, el) => {
            times.push($(el).text())
        })
        if (page < pageLimit + 1) {
            runningGetHtml(guName, code)
        } else {
            console.log(guName, code, '123')
            writeDir(guName, code)
        }
    }).catch(err => {
        if (err === 'error-fetch') {
            pageTimer = setTimeout(() => {
                titles = []
                times = []
                page = 1
                // ++count
                // clearTimeout(timer)
                caclCount()
            }, 3000)
        }

    }).finally(() => {

    })
    // 5-12
    // 4-8
}
const caclCount = () => {
    runningGetHtml(excel_content[count - 1][2], excel_content[count - 1][1])
}


